package com.example.sb.controller;

import com.example.sb.common.Result;
import com.example.sb.entity.User;
import com.example.sb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserControllar {



    @Autowired
    UserService userService;


    @PostMapping("/add")
    public Result add(@RequestBody User user) {
        try {
            userService.insertuser(user);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据库错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }


    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        userService.updateuser(user);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        userService.deleteUser(id);
        return Result.success();
    }


}
